﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarOccupationTypeDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarOccupationTypeDB
    {
        public CarOccupationTypeDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarOccupationType caroccupationtype)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarOccupationTypeInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CarOccupationTypeId", caroccupationtype.CarOccupationTypeId);
            _sPara.Add("@ProviderCompanyId", caroccupationtype.ProviderCompanyId);
            _sPara.Add("@TypeId", caroccupationtype.TypeId);
            _sPara.Add("@Description", caroccupationtype.Description);
            //_sPara.Add("@Code", caroccupationtype.Code);
            _sPara.Add("@OccupationTypeRate", caroccupationtype.OccupationTypeRate);
            _sPara.Add("@RefCEUserId", caroccupationtype.RefCEUserId);
            _sPara.Add("@Status", caroccupationtype.Status);
            _sPara.Add("@Finfo1", caroccupationtype.FInfo1);
            _sPara.Add("@Dinfo1", caroccupationtype.DInfo1);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Update()
        {
            return true;
        }

        public bool Delete(Int64 CarOccupationTypeId, Int64 ProviderCompanyId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarOccupationTypeDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CarOccupationTypeId", CarOccupationTypeId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }

        public CarOccupationType GetCarOccupationType(Int64 ProviderCompanyId, Int32 TypeId)
        {
            CarOccupationType _OccupationType = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarOccupationTypeSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@TypeId", TypeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _OccupationType = new CarOccupationType();
                    _OccupationType = FillDataRecord(_dt, 0);
                }
            }
            return _OccupationType;
        }

        public CarOccupationType GetCarOccupationType(Int64 CarOccupationTypeId)
        {
            CarOccupationType _OccupationType = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarOccupationTypeSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@CarOccupationTypeId", CarOccupationTypeId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _OccupationType = new CarOccupationType();
                    _OccupationType = FillDataRecord(_dt, 0);
                }
            }
            return _OccupationType;
        }

        private CarOccupationType FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarOccupationType myOccupationType = new CarOccupationType();
            myOccupationType.CarOccupationTypeId = Convert.ToInt64(myDataTable.Rows[RowId]["CarOccupationTypeId"].ToString());
            myOccupationType.ProviderCompanyId = Convert.ToInt64(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myOccupationType.Description = myDataTable.Rows[RowId]["Description"].ToString();
            myOccupationType.TypeId = Convert.ToInt32(myDataTable.Rows[RowId]["TypeId"].ToString());
            myOccupationType.OccupationTypeRate = Convert.ToSingle(myDataTable.Rows[RowId]["OccupationTypeRate"].ToString());
            //myOccupationType.Code = Convert.ToInt32(myDataTable.Rows[RowId]["Code"].ToString());
            myOccupationType.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myOccupationType.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            myOccupationType.CreatedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["CreatedDate"].ToString());
            myOccupationType.ModifiedDate = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());

            if (myDataTable.Rows[RowId]["FInfo1"].ToString() != "")
                myOccupationType.FInfo1 = Convert.ToSingle(myDataTable.Rows[RowId]["FInfo1"].ToString());

            if (myDataTable.Rows[RowId]["DInfo1"].ToString() != "")
                myOccupationType.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["DInfo1"].ToString());
            else
                myOccupationType.DInfo1 = Convert.ToDateTime(myDataTable.Rows[RowId]["ModifiedDate"].ToString());
            return myOccupationType;
        }
        
    }
}